var models = require('../models'),
  List = models.List;
  var Tag = models.Tag;

exports.getListById = function (id, callback) {
  List.findOne({_id: id}, callback);
};

exports.getListByName = function (name, callback) {
  List.findOne({name: name}, callback);
};

/**
 * 不同用户可以有相同名字的分类，但同一个用户不可！
 * @param  {[type]}   name     [description]
 * @param  {[type]}   user_id  [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
exports.getListByNameAndUserId = function (name, user_id, callback) {
  List.findOne({name: name, user_id: user_id}, callback);
};

exports.getListsByUserId = function (user_id, callback) {
  List.find({user_id: user_id}, callback);
};

exports.newAndSave = function (name, desc, order, user_id, tag_id, callback) {
  var list = new List();
  list.name = name;
  list.desc = desc;
  list.order = order;
  list.user_id = user_id;
  list.tag_id = tag_id;
  list.save(callback);
};

exports.getCount = function (callback) {
  List.count({}, callback);
}


exports.getTagByName = function (name, callback) {
  Tag.findOne({name: name}, callback);
};

/**
 * 根据标签ID列表，获取一组标签
 * Callback:
 * - err, 数据库异常
 * - tags, 标签列表
 * @param {Array} ids 标签ID列表
 * @param {Function} callback 回调函数
 */
exports.getTagsByIds = function (ids, callback) {
  Tag.find({_id: {'$in': ids}}, callback);
};

/**
 * 获取所有标签
 * Callback:
 * - err, 数据库异常
 * - tags, 标签列表
 * @param {Function} callback 回调函数
 */
exports.getAllTags = function (callback) {
  Tag.find({}, [], {sort: [['order', 'asc']]}, callback);
};

/**
 * 根据标签ID获取标签
 * Callback:
 * - err, 数据库异常
 * - tag, 标签
 * @param {String} id 标签ID
 * @param {Function} callback 回调函数
 */
exports.getTagById = function (id, callback) {
  Tag.findOne({_id: id}, callback);
};

exports.update = function (tag, name, order, callback) {
  tag.name = name;
  tag.order = order;
  tag.save(callback);
};

exports.newAndSave2 = function (name, order, callback) {
  var tag = new Tag();
  tag.name = name;
  tag.order = order;
  tag.save(callback);
};
